package account.provider;

import java.io.IOException;
import java.time.Instant;
import java.util.Properties;

import account.Account;
import aimee.common.Logger;
import aimee.common.SQLConnection;

/**
 * 实现查询接口QueryProvider
 * 
 * @author 毛建杰
 *
 */
public class QueryProviderImpl implements QueryProvider
{
	private static Properties properties;
	private static String jdbcDriver;

	static
	{
		try
		{
			properties = new Properties();
			properties.load(QueryProviderImpl.class.getResourceAsStream("/account.properties"));
			jdbcDriver = "com.mysql.cj.jdbc.Driver";
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public int queryUserID(String userName)
	{
		String url = properties.getProperty("mysql.account.url");
		String user = properties.getProperty("mysql.account.user");
		String password = properties.getProperty("mysql.account.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			String sql = "select uid from user_index where uname = '" + userName + "'";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return -1;
			}
			else
			{
				return Integer.parseInt(data[0][0]);
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return -1;
	}

	@Override
	public String queryRealName(int userID)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = userID % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "select realname from " + tableName + " where uid = '" + userID + "'";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return "";
			}
			else
			{
				return data[0][0];
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return "";
	}

	@Override
	public String queryIdcard(int userID)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = userID % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "select idcard from " + tableName + " where uid = '" + userID + "'";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return "";
			}
			else
			{
				return data[0][0];
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return "";
	}

	@Override
	public Instant queryRegisterTime(int userID)
	{
		String url = properties.getProperty("mysql.account.url");
		String user = properties.getProperty("mysql.account.user");
		String password = properties.getProperty("mysql.account.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			String sql = "select regdate from user_index where uid = '" + userID + "'";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return null;
			}
			else
			{
				return Instant.ofEpochSecond(Long.valueOf(data[0][0]), 0);
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public String queryRegisterIP(int userID)
	{
		String url = properties.getProperty("mysql.account.url");
		String user = properties.getProperty("mysql.account.user");
		String password = properties.getProperty("mysql.account.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			String sql = "select regip from user_index where uid = '" + userID + "'";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return "";
			}
			else
			{
				return Account.long2Ip(Long.parseLong(data[0][0]));
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return "";
	}

	@Override
	public String queryPhone(int userID)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = userID % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "select phone from " + tableName + " where uid = '" + userID + "' ";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return "";
			}
			else
			{
				return data[0][0];
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return "";
	}

	@Override
	public String queryEmail(int userID)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = userID % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "select email from " + tableName + " where uid = '" + userID + "' ";
			String[][] data = sqlConnection.executeQuery(sql);
			if (data == null || data.length == 0 || data[0] == null || data[0].length == 0 || data[0][0] == null
					|| data[0][0].isEmpty())
			{
				return "";
			}
			else
			{
				return data[0][0];
			}
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return "";
	}
}
